package cn.xsly.org.controller;

import cn.xsly.basic.exception.BusinessException;
import cn.xsly.basic.util.ExcelUtils;
import cn.xsly.basic.util.PageList;
import cn.xsly.basic.util.AjaxResult;
import cn.xsly.org.domain.Shop;
import cn.xsly.org.domain.ShopAuditLog;
import cn.xsly.org.domain.ShopDto;
import cn.xsly.org.query.ShopQuery;
import cn.xsly.org.service.ShopService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController

@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private ShopService shopService;

    /*查询所有*/
    @GetMapping
    public List<Shop> findAll() {
        return shopService.findAll();
    }

    /*查询单个*/
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id") Long id) {
        return shopService.findById(id);
    }

    /*分页查询*/
    @PostMapping
    public PageList<Shop> queryPageList(@RequestBody ShopQuery employeeQuery) {
        return shopService.queryPage(employeeQuery);
    }

    /*删除*/
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {


        try {
            shopService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "失败");
        }
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop employee){
        try {
            if(employee.getId()==null){
                shopService.add(employee);
            }else {
                shopService.update(employee);
            }
            /*成功返回静态值*/
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"失败了");
        }
    }

    @PatchMapping
    @ApiOperation(value="批量删除接口")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            shopService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @ApiOperation(value = "商家入驻",notes ="")
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {
            return AjaxResult.me().setSuccess(false).setMsg("入驻失败" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统异常，请重试");
        }
    }

    /**
     * 接口：店铺审核成功
     * @param shopAuditLog
     * @return AjaxResult 响应给前端
     */
    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.auditPass(shopAuditLog);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试");
        }
    }

    /**
     * 接口：店铺审核失败
     * @param shopAuditLog
     * @return AjaxResult 响应给前端
     */
    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.auditReject(shopAuditLog);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试!!!");
        }
    }

     @GetMapping("/export")
    public void export(HttpServletResponse response) {
         try {
             List<Shop> shops = shopService.findAll();
             ExcelUtils.exportExcel(shops, null, "店铺名称", Shop.class, "shop.xlsx", response);
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
         @PostMapping("/importExcel")
         public void importExcel(@RequestPart("file") MultipartFile file){
             //MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass
             //0表示标题的行数    1表示表头的行数
             List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);
             //可以根据需求批量添加到数据库
             shops.forEach(System.out::println);
         }

    @GetMapping("/echarts")
    public List<ShopDto> echarts(){
        return shopService.echarts();
    }
    @GetMapping("/active/{id}")
    public AjaxResult active(@PathVariable("id") Long id) {
        try {

            shopService.active(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"失败了");
        }
    }

     }


